import * as THREE from "three";
import { ThreeObjectComponent } from "../component/ThreeObjectComponent";
import { SizeComponent } from "../component/SizeComponent";
import { PositionComponent } from "../component/PositionComponent";
import { GameWorld } from "../world/World";
import { SystemBase } from "./SystemBase";


export class GameObjectSystem extends SystemBase {
    constructor(world: GameWorld) {
        super(world);
    }
    Add(e: ThreeObjectComponent, position: PositionComponent, size: SizeComponent) {
        const geometry = new THREE.BoxBufferGeometry(1, 1.5, 1);
        geometry.scale(size.value, size.value, 1);
        const mat = new THREE.MeshStandardMaterial();
        mat.color = new THREE.Color(0x88ff00);
        e.mesh = new THREE.Mesh(geometry, mat);
        this.world.scene.add(e.mesh);
        this.Update(e, position, size);
    }

    Remove(go: ThreeObjectComponent) {
        go.mesh.parent?.remove(go.mesh);
        go.destroyed = true;
        go.mesh = null;
        this.world.scene.remove(go.mesh);
    }

    Update(go: ThreeObjectComponent, position: PositionComponent, size: SizeComponent) {
        go.mesh.position.copy(position.value);
        go.mesh.geometry.scale(size.value, size.value, 1);

    }

}
